﻿@page "/itool"

@inject IToolCenter toolCenter
@if (model != null)
{


    <Form Model="@model"
          LabelColSpan="4"
          WrapperColSpan="16"
          OnFinish="OnFinish"
          OnFinishFailed="OnFinishFailed">

        <FormItem Label="Port">
            <Input @bind-Value="@context.Port" />
        </FormItem>
        <FormItem Label="Server Port">
            <Input @bind-Value="@context.ServicePort" Disabled="true" />
        </FormItem>
        <FormItem Label="Server Gateway Port">
            <Input @bind-Value="@context.ServiceGatewayPort" Disabled="true" />
        </FormItem>
        <FormItem Label="Accept Queue Count">
            <Input @bind-Value="@context.SoBacklog" />
        </FormItem>
        <FormItem Label="Heartbeat Times">
            <Input @bind-Value="@context.HeartbeatTimeOutSeconds" />
        </FormItem>

        <FormItem Label="Database Provider">
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select iToolBase"
                    DataSource="@context.BaseSQLServerSelects"
                    @bind-Values="@context.BaseSQLServer"
                    DefaultActiveFirstOption="true"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select Function"
                    DataSource="@context.FnsSQLServerSelects"
                    @bind-Values="@context.FnsSQLServer"
                    DefaultActiveFirstOption="true"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select Message"
                    DataSource="@context.MsgSQLServerSelects"
                    @bind-Values="@context.MsgSQLServer"
                    DefaultActiveFirstOption="true"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select Logger"
                    DataSource="@context.LogSQLServerSelects"
                    @bind-Values="@context.LogSQLServer"
                    DefaultActiveFirstOption="true"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
        </FormItem>

        <FormItem Label="MongoDB Provider">
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select MongoDB"
                    DataSource="@context.MongoDBSelects"
                    @bind-Values="@context.MongoDB"
                    OnSelectedItemsChanged="@(item => OnSelectedItemChangedHandler(item, "MongoDB"))"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
        </FormItem>

        <FormItem Label="ElasticSearch Provider">
            <Select Mode="multiple" Style="width:24%;" Placeholder="Please Select ElasticSearch"
                    DataSource="@context.ElasticsearchSelects"
                    @bind-Values="@context.ElasticSearch"
                    OnSelectedItemsChanged="@(item => OnSelectedItemChangedHandler(item, "Elasticsearch"))"
                    GroupName="@nameof(SelectOption.IP)"
                    LabelName="@nameof(SelectOption.Label)"
                    ValueName="@nameof(SelectOption.Value)">
            </Select>
        </FormItem>

        <FormItem WrapperColOffset="4" WrapperColSpan="16">
            <Button HtmlType="submit">
                Submit
            </Button>
        </FormItem>
    </Form>
}

@code
{
    WindowsHelper windows;
    iToolCenterOptionRequset model;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await base.InvokeAsync(async () =>
            {
                windows = new WindowsHelper(jSRuntime);
                model = await toolCenter.Get() ?? new iToolCenterOptionRequset();
                await windows.HideLoadingAsync();
                await windows.SetTitleAsync("iTool Center Edit");
                base.StateHasChanged();
            });
        }
    }

    private void OnSelectedItemChangedHandler(IEnumerable<SelectOption> value, string process)
    {
        if (value.Count() == 0)
        {
            return;
        }
        switch (process)
        {
            case "MongoDB":
                model.MongoDB = new List<string> { value.Last().Value };
                break;
            case "Elasticsearch":
                model.ElasticSearch = new List<string> { value.Last().Value };
                break;
        }

        base.StateHasChanged();
    }
}

@code
{

    private async void OnFinish(EditContext editContext)
    {
        string error = string.Empty;

        if (!model.BaseSQLServer.Any())
        {
            error = "Please Select iTool Base Server";
        }
        if (!model.FnsSQLServer.Any())
        {
            error = "Please Select Function Server";
        }

        if (!model.MsgSQLServer.Any())
        {
            error = "Please Select Message Server";
        }
        if (!model.LogSQLServer.Any())
        {
            error = "Please Select Logger Server";
        }
        if (!model.MongoDB.Any())
            error = "Please Select MongoDB Server";

        if (!model.ElasticSearch.Any())
            error = "Please Select ElasticSearch Server";


        if (string.IsNullOrWhiteSpace(error))
        {
            var result = await toolCenter.Edit(model);
            if (result.IsSuccessful)
            {
                var task = notice.Success(new NotificationConfig
                {
                    Message = "保存成功"
                });
            }
            else
            {
                var task = notice.Error(new NotificationConfig
                {
                    Message = "操作失败请重试",
                    Description = result.Description
                });
            }
        }
        else
        {
            var task = notice.Error(new NotificationConfig
            {
                Message = "数据验证失败",
                Description = error
            });
        }
    }

    private void OnFinishFailed(EditContext editContext)
    {
       _ = windows.ConsoleErrorAsync(model);
    }

}
